package com.corner.ui.base;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.corner.app.App;
import com.corner.presenter.base.BasePresenter;
import com.corner.ui.view.base.BaseView;
import com.corner.utils.ToastUtil;
import com.corner.utils.view.InjectViewUtils;

import butterknife.ButterKnife;

/**
 * Fragment的基类
 *
 * @author Administrator
 */
public abstract class BaseFragment<P extends BasePresenter> extends Fragment implements BaseView {

    protected P presenter;
    protected BaseActivity activity;
    protected View view;

    protected abstract P createP();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = createP();
        if (presenter != null) {
            presenter.setView(this);
        }
        activity = (BaseActivity) getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = initView(inflater);
        InjectViewUtils.injectView(this, view, activity);
        ButterKnife.inject(this, view);
        setOnclick();
        return view;
    }

    protected abstract void setOnclick();

    protected abstract View initView(LayoutInflater inflater);

    @Override
    public void onResume() {
        super.onResume();
        if (presenter != null) presenter.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (presenter != null) presenter.onPause();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (presenter != null) presenter.onDestroy();
        presenter = null;
        activity = null;
        view = null;
    }

    @Override
    public void showLoading(boolean isShow) {
        if (isShow) {
            activity.showProgressDialog();
        } else {
            activity.hideProgressDialog();
        }
    }

    @Override
    public void showError(String message) {
        ToastUtil.showCustomToastInDevelop(App.application, message);
    }
}
